■ PIC32MZ CDCクラス 文字列の送受信(Harmony キャラクタ液晶)
PIC32MZ2048ECH144にキャラクタ液晶を接続した USB通信 送受信の例を紹介します。 → PC側 VC# ソフト
<試作品仕様>
・PCとPIC32MZ間で 文字列の送受信をおこなう。
・USBのライブラリはHarmonyのCDCクラスをつかうこと
・PC側から文字列に対して PIC側からそれぞれに対応した文字列を返信すること
送受信の文字列は 以下とする
PCからの送信文字列 PICからの返信文字列
Japan\r Tokyo
America\r Washington
U.K.\r London
France\r Paris
・PICにはキャラくタ液晶を接続し 受信文字列と返信文字列を表示すること
・PIC側のダイアログのテキストボックスに受信文字列を表示のこと
・ 開発環境 PIC側: Harmony ver. 1.03 XC32 ver.1.34 MPLABX ver.2.35 PIC32MZ2048ECH144
revision 5
PC側: VC# 2012
<試作品回路図>(→回路図のPDFファイル)
PIC32MZ2048ECH144をつかった場合の回路図を以下に示します。
<試作品外観>下記の写真には上記回路図にはない、また本テーマと関係のない部品が多々写っています
<プログラム例> //以下、main.c //----------------------------------------------------------------------------- /******************************************************************************* MPLAB Harmony Project Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This file contains the "main" function for an MPLAB Harmony project. Description: This file contains the "main" function for an MPLAB Harmony project. The "main" function calls the "SYS_Initialize" function to initialize the state machines of all MPLAB Harmony modules in the system and it calls the "SYS_Tasks" function from within a system-wide "super" loop to maintain their correct operation. These two functions are implemented in configuration-specific files (usually "system_init.c" and "system_tasks.c") in a configuration-specific folder under the "src/system_config" folder within this project's top-level folder. An MPLAB Harmony project may have more than one configuration, each contained within it's own folder under the "system_config" folder. *******************************************************************************/ // DOM-IGNORE-BEGIN /******************************************************************************* Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved. //Microchip licenses to you the right to use, modify, copy and distribute Software only when embedded on a Microchip microcontroller or digital signal controller that is integrated into your product or third party product (pursuant to the sublicense terms in the accompanying license agreement). You should refer to the license agreement accompanying this Software for additional information regarding your rights and obligations. SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. *******************************************************************************/ // DOM-IGNORE-END // ***************************************************************************** // ***************************************************************************** // Section: Included Files // ***************************************************************************** // ***************************************************************************** #include <stddef.h> // Defines NULL #include <stdbool.h> // Defines true #include <stdlib.h> // Defines EXIT_FAILURE #include "system/common/sys_module.h" // SYS function prototypes // ***************************************************************************** // ***************************************************************************** // Section: Main Entry Point // ***************************************************************************** // ***************************************************************************** int main ( void ) { /* Initialize all MPLAB Harmony modules, including application(s). */ SYS_Initialize ( NULL ); while ( true ) { /* Maintain state machines of all polled MPLAB Harmony modules. */ SYS_Tasks ( ); } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); } /******************************************************************************* End of File */ //以下、app.c //-------------------------------------------------------------------------------- /******************************************************************************* MPLAB Harmony Application Source File Company: Microchip Technology Inc. File Name: app.c Summary: This file contains the source code for the MPLAB Harmony application. Description: This file contains the source code for the MPLAB Harmony application. It implements the logic of the application's state machine and it may call API routines of other MPLAB Harmony modules in the system, such as drivers, system services, and middleware. However, it does not call any of the system interfaces (such as the "Initialize" and "Tasks" functions) of any of the modules in the system or make any assumptions about when those functions are called. That is the responsibility of the configuration-specific system files. *******************************************************************************/ // DOM-IGNORE-BEGIN /******************************************************************************* Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved. Microchip licenses to you the right to use, modify, copy and distribute Software only when embedded on a Microchip microcontroller or digital signal controller that is integrated into your product or third party product (pursuant to the sublicense terms in the accompanying license agreement). You should refer to the license agreement accompanying this Software for additional information regarding your rights and obligations. SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. *******************************************************************************/ // DOM-IGNORE-END // ***************************************************************************** // ***************************************************************************** // Section: Included Files // ***************************************************************************** // ***************************************************************************** #include "app.h" #include "1lcd_lib_C32.h" int delay_Clock = 200000000; //200MHz char Buf[17]; //1液晶表示バッファー int ix = 0; char* str; char buf[20]; char str3[20]; char* str2; char Japan[] = "Japan\r"; char America[] = "America\r"; char UK[] = "U.K.\r"; char France[] = "France\r"; char IamFine[] = "I am fine\r"; char Tokyo[] = "Tokyo"; char Washington[] = "Washington"; char London[] = "London"; char Paris[] = "Paris"; char Pardon[] = "Pardon ?"; //uint8_t APP_MAKE_BUFFER_DMA_READY IamFine[] = "\r\nI am fine"; uint8_t APP_MAKE_BUFFER_DMA_READY _Ans[] = " "; uint8_t APP_MAKE_BUFFER_DMA_READY _Tokyo[] = "\r\nTokyo"; uint8_t APP_MAKE_BUFFER_DMA_READY _Washington[] = "\r\nWashington"; uint8_t APP_MAKE_BUFFER_DMA_READY _London[] = "\r\nLondon"; uint8_t APP_MAKE_BUFFER_DMA_READY _Paris[] = "\r\nParis"; uint8_t APP_MAKE_BUFFER_DMA_READY _Pardon[] = "\r\nPardon ?"; void delay_us(volatile unsigned int usec) //1μsec遅延 { volatile int count; count = (int)(delay_Clock/20000000)*usec; do //実測 at 200MH (Clock=200000000) { //delay_us(1000):1000.4μsec delay_us(100):100.6μsec delay_us(10):10.5μsec delay_us(1):1.5μsec asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); count--; }while(count != 0); } void delay_ms(volatile unsigned int msec) //1msec遅延 { volatile unsigned int i; //実測:at200MH (Clock=200000000)//delay_ms(1): 1.0006msec delay_ms(100):100.04msec for(i=0; i<msec; i++) delay_us(1000); } // ***************************************************************************** // ***************************************************************************** // Section: Global Data Definitions // ***************************************************************************** // ***************************************************************************** uint8_t APP_MAKE_BUFFER_DMA_READY switchPromptUSB[] = "\r\nHellow World !! "; //uint8_t APP_MAKE_BUFFER_DMA_READY switchPromptUSB[] = "\r\nPUSH BUTTON PRESSED"; uint8_t APP_MAKE_BUFFER_DMA_READY readBuffer[APP_READ_BUFFER_SIZE]; // ***************************************************************************** /* Application Data Summary: Holds application data Description: This structure holds the application's data. Remarks: This structure should be initialized by the APP_Initialize function. Application strings and buffers are be defined outside this structure. */ APP_DATA appData; // ***************************************************************************** // ***************************************************************************** // Section: Application Callback Functions // ***************************************************************************** // ***************************************************************************** /******************************************************* * USB CDC Device Events - Application Event Handler *******************************************************/ USB_DEVICE_CDC_EVENT_RESPONSE APP_USBDeviceCDCEventHandler ( USB_DEVICE_CDC_INDEX index , USB_DEVICE_CDC_EVENT event , void * pData, uintptr_t userData ) { APP_DATA * appDataObject; appDataObject = (APP_DATA *)userData; USB_CDC_CONTROL_LINE_STATE * controlLineStateData; switch ( event ) { case USB_DEVICE_CDC_EVENT_GET_LINE_CODING: /* This means the host wants to know the current line * coding. This is a control transfer request. Use the * USB_DEVICE_ControlSend() function to send the data to * host. */ USB_DEVICE_ControlSend(appDataObject->deviceHandle, &appDataObject->getLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_LINE_CODING: /* This means the host wants to set the line coding. * This is a control transfer request. Use the * USB_DEVICE_ControlReceive() function to receive the * data from the host */ USB_DEVICE_ControlReceive(appDataObject->deviceHandle, &appDataObject->setLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE: /* This means the host is setting the control line state. * Read the control line state. We will accept this request * for now. */ controlLineStateData = (USB_CDC_CONTROL_LINE_STATE *)pData; appDataObject->controlLineStateData.dtr = controlLineStateData->dtr; appDataObject->controlLineStateData.carrier = controlLineStateData->carrier; USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_SEND_BREAK: /* This means that the host is requesting that a break of the * specified duration be sent. Read the break duration */ appDataObject->breakData = ((USB_DEVICE_CDC_EVENT_DATA_SEND_BREAK *)pData)->breakDuration; break; case USB_DEVICE_CDC_EVENT_READ_COMPLETE: /* This means that the host has sent some data*/ appDataObject->isReadComplete = true; break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* The data stage of the last control transfer is * complete. For now we accept all the data */ USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT: /* This means the GET LINE CODING function data is valid. We dont * do much with this data in this demo. */ break; case USB_DEVICE_CDC_EVENT_WRITE_COMPLETE: /* This means that the data write got completed. We can schedule * the next read. */ appDataObject->isWriteComplete = true; break; default: break; } return USB_DEVICE_CDC_EVENT_RESPONSE_NONE; } /*********************************************** * Application USB Device Layer Event Handler. ***********************************************/ void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context ) { USB_DEVICE_EVENT_DATA_CONFIGURED *configuredEventData; switch ( event ) { case USB_DEVICE_EVENT_SOF: /* This event is used for switch debounce. This flag is reset * by the switch process routine. */ appData.sofEventHasOccurred = true; break; case USB_DEVICE_EVENT_RESET: /* Update LED to show reset state */ BSP_LEDOn ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOff ( APP_USB_LED_3 ); appData.isConfigured = false; break; case USB_DEVICE_EVENT_CONFIGURED: /* Check the configuratio. We only support configuration 1 */ configuredEventData = (USB_DEVICE_EVENT_DATA_CONFIGURED*)eventData; if ( configuredEventData->configurationValue == 1) { /* Update LED to show configured state */ BSP_LEDOff ( APP_USB_LED_1 ); BSP_LEDOff ( APP_USB_LED_2 ); BSP_LEDOn ( APP_USB_LED_3 ); /* Register the CDC Device application event handler here. * Note how the appData object pointer is passed as the * user data */ USB_DEVICE_CDC_EventHandlerSet(USB_DEVICE_CDC_INDEX_0, APP_USBDeviceCDCEventHandler, (uintptr_t)&appData); /* Mark that the device is now configured */ appData.isConfigured = true; } break; case USB_DEVICE_EVENT_POWER_DETECTED: /* VBUS was detected. We can attach the device */ USB_DEVICE_Attach(appData.deviceHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS is not available any more. Detach the device. */ USB_DEVICE_Detach(appData.deviceHandle); break; case USB_DEVICE_EVENT_SUSPENDED: /* Switch LED to show suspended state */ BSP_LEDOff ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOn ( APP_USB_LED_3 ); break; case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } } // ***************************************************************************** // ***************************************************************************** // Section: Application Local Functions // ***************************************************************************** // ***************************************************************************** void APP_ProcessSwitchPress() { /* This function checks if the switch is pressed and then * debounces the switch press*/ if(BSP_SWITCH_STATE_PRESSED == (BSP_SwitchStateGet(APP_USB_SWITCH_1))) { if(appData.ignoreSwitchPress) { /* This measn the key press is in progress */ if(appData.sofEventHasOccurred) { /* A timer event has occurred. Update the debounce timer */ appData.switchDebounceTimer ++; appData.sofEventHasOccurred = false; if (USB_DEVICE_ActiveSpeedGet(appData.deviceHandle) == USB_SPEED_FULL) { appData.debounceCount = APP_USB_SWITCH_DEBOUNCE_COUNT_FS; } else if (USB_DEVICE_ActiveSpeedGet(appData.deviceHandle) == USB_SPEED_HIGH) { appData.debounceCount = APP_USB_SWITCH_DEBOUNCE_COUNT_HS; } if(appData.switchDebounceTimer == appData.debounceCount) { /* Indicate that we have valid switch press. The switch is * pressed flag will be cleared by the application tasks * routine. We should be ready for the next key press.*/ appData.isSwitchPressed = true; appData.switchDebounceTimer = 0; appData.ignoreSwitchPress = false; } } } else { /* We have a fresh key press */ appData.ignoreSwitchPress = true; appData.switchDebounceTimer = 0; } } else { /* No key press. Reset all the indicators. */ appData.ignoreSwitchPress = false; appData.switchDebounceTimer = 0; appData.sofEventHasOccurred = false; } } /***************************************************** * This function is called in every step of the * application state machine. *****************************************************/ bool APP_StateReset(void) { /* This function returns true if the device * was reset */ bool retVal; if(appData.isConfigured == false) { appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; appData.readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; appData.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; appData.isReadComplete = true; appData.isWriteComplete = true; retVal = true; } else { retVal = false; } return(retVal); } // ***************************************************************************** // ***************************************************************************** // Section: Application Initialization and State Machine Functions // ***************************************************************************** // ***************************************************************************** /******************************************************************************* Function: void APP_Initialize ( void ) Remarks: See prototype in app.h. */ void APP_Initialize ( void ) { /* Place the App state machine in its initial state. */ appData.state = APP_STATE_INIT; /* Device Layer Handle */ appData.deviceHandle = USB_DEVICE_HANDLE_INVALID ; /* Device configured status */ appData.isConfigured = false; /* Initial get line coding state */ appData.getLineCodingData.dwDTERate = 9600; appData.getLineCodingData.bParityType = 0; appData.getLineCodingData.bParityType = 0; appData.getLineCodingData.bDataBits = 8; /* Read Transfer Handle */ appData.readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; /* Write Transfer Handle */ appData.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; /* Intialize the read complete flag */ appData.isReadComplete = true; /*Initialize the write complete flag*/ appData.isWriteComplete = true; /* Initialize Ignore switch flag */ appData.ignoreSwitchPress = false; /* Reset the switch debounce counter */ appData.switchDebounceTimer = 0; /* Reset other flags */ appData.sofEventHasOccurred = false; appData.isSwitchPressed = false; /* Set up the read buffer */ appData.readBuffer = &readBuffer[0]; lcd_init(); // LCD初期化 lcd_cmd(0b00001100); // カーソル:OFF ブリンク:OFF lcd_cmd(0x80); //1目の先頭へ sprintf(Buf,"USB CDC Start !! ");// lcd_str(Buf); //液晶表示 lcd_cmd(0xC0); //2行目の先頭へ sprintf(Buf," "); // lcd_str(Buf); // 開始メッセージ1行目表示 delay_ms(2000); } /****************************************************************************** Function: void APP_Tasks ( void ) Remarks: See prototype in app.h. */ void APP_Tasks (void ) { /* Update the application state machine based * on the current state */ USB_DEVICE_CDC_RESULT result; switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData.isConfigured) { /* If the device is configured then lets start reading */ appData.state = APP_STATE_SCHEDULE_READ; } break; case APP_STATE_SCHEDULE_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else wait for the current read to complete */ appData.state = APP_STATE_WAIT_FOR_READ_COMPLETE; if(appData.isReadComplete == true) { appData.isReadComplete = false; appData.readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0, &appData.readTransferHandle, appData.readBuffer, APP_READ_BUFFER_SIZE); if(appData.readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID) { appData.state = APP_STATE_ERROR; break; } } break; case APP_STATE_WAIT_FOR_READ_COMPLETE: case APP_STATE_CHECK_SWITCH_PRESSED: if(APP_StateReset()) { break; } APP_ProcessSwitchPress(); /* Check if a character was received or a switch was pressed. * The isReadComplete flag gets updated in the CDC event handler. */ if(appData.isReadComplete || appData.isSwitchPressed) { appData.state = APP_STATE_SCHEDULE_WRITE; } break; case APP_STATE_SCHEDULE_WRITE: if(APP_StateReset()) { break; } /* Setup the write */ appData.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; appData.isWriteComplete = false; appData.state = APP_STATE_WAIT_FOR_WRITE_COMPLETE; if(appData.isSwitchPressed) { /* If the switch was pressed, then send the switch prompt*/ appData.isSwitchPressed = false; result = USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0, &appData.writeTransferHandle, switchPromptUSB, 23, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); } else { if(strcmp(readBuffer,Japan) == 0) { str = _Tokyo; str2 = &Tokyo[0]; } else if(strcmp(readBuffer,America) == 0) { str = _Washington; str2 = &Washington[0]; } else if(strcmp(readBuffer,UK) == 0) { str = _London; str2 = &London[0]; } else if(strcmp(readBuffer,France) == 0) { str = _Paris; str2 = &Paris[0]; } else if(strcmp(readBuffer,IamFine) == 0) { str = _Pardon; str2 = &Pardon[0]; } else { str = _Pardon; str2 = &Pardon[0]; } lcd_cmd(0x80); //1目の先頭へ sprintf(Buf,"%s ",readBuffer);// lcd_str(Buf); //液晶表示 lcd_cmd(0xC0); //2行目の先頭へ // sprintf(Buf," "); //うまく働かない for(ix = 0; ix < 20; ix++)Buf[ix] = 0x20; lcd_str(Buf); // 開始メッセージ1行目表示 lcd_cmd(0xC0); //2行目の先頭へ sprintf(Buf,"%s",str2); // lcd_str(Buf); // 開始メッセージ1行目表示 ix = 0; for(ix = 0; ix < 20; ix++)readBuffer[ix] = '\0'; //バッファークリア result = USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0, &appData.writeTransferHandle,str, 12, //送信文字変更 // &appData.writeTransferHandle, IamFine, 11, //送信文字変更 USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); /* appData.readBuffer[0] = appData.readBuffer[0] + 1; USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0, &appData.writeTransferHandle, appData.readBuffer, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); */ } break; case APP_STATE_WAIT_FOR_WRITE_COMPLETE: if(APP_StateReset()) { break; } /* Check if a character was sent. The isWriteComplete * flag gets updated in the CDC event handler */ if(appData.isWriteComplete == true) { appData.state = APP_STATE_SCHEDULE_READ; } break; case APP_STATE_ERROR: break; default: break; } } /******************************************************************************* End of File */ //以下、system_init.c //------------------------------------------------------------------------------ /******************************************************************************* System Initialization File File Name: system_init.c Summary: This file contains source code necessary to initialize the system. Description: This file contains source code necessary to initialize the system. It implements the "SYS_Initialize" function, configuration bits, and allocates any necessary global system resources, such as the systemObjects structure that contains the object handles to all the MPLAB Harmony module objects in the system. *******************************************************************************/ // DOM-IGNORE-BEGIN /******************************************************************************* Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved. Microchip licenses to you the right to use, modify, copy and distribute Software only when embedded on a Microchip microcontroller or digital signal controller that is integrated into your product or third party product (pursuant to the sublicense terms in the accompanying license agreement). You should refer to the license agreement accompanying this Software for additional information regarding your rights and obligations. SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. *******************************************************************************/ // DOM-IGNORE-END // ***************************************************************************** // ***************************************************************************** // Section: Included Files // ***************************************************************************** // ***************************************************************************** #include "system_config.h" #include "system_definitions.h" #include "app.h" // **************************************************************************** // **************************************************************************** // Section: Configuration Bits // **************************************************************************** // **************************************************************************** /*** DEVCFG0 ***/ #pragma config DEBUG = OFF #pragma config JTAGEN = OFF #pragma config ICESEL = ICS_PGx2 #pragma config TRCEN = OFF #pragma config BOOTISA = MIPS32 #pragma config FECCCON = OFF_UNLOCKED #pragma config FSLEEP = OFF #pragma config DBGPER = PG_ALL #pragma config EJTAGBEN = NORMAL #pragma config CP = OFF /*** DEVCFG1 ***/ #pragma config FNOSC = SPLL #pragma config DMTINTV = WIN_127_128 #pragma config FSOSCEN = OFF #pragma config IESO = OFF #pragma config POSCMOD = EC #pragma config OSCIOFNC = OFF #pragma config FCKSM = CSDCMD #pragma config WDTPS = PS1048576 #pragma config WDTSPGM = STOP #pragma config FWDTEN = OFF #pragma config WINDIS = NORMAL #pragma config FWDTWINSZ = WINSZ_25 #pragma config DMTCNT = DMT31 #pragma config FDMTEN = OFF /*** DEVCFG2 ***/ #pragma config FPLLIDIV = DIV_3 #pragma config FPLLRNG = RANGE_5_10_MHZ #pragma config FPLLICLK = PLL_POSC #pragma config FPLLMULT = MUL_50 #pragma config FPLLODIV = DIV_2 #pragma config UPLLFSEL = FREQ_24MHZ #pragma config UPLLEN = ON /*** DEVCFG3 ***/ #pragma config USERID = 0xffff #pragma config FMIIEN = OFF #pragma config FETHIO = OFF #pragma config PGL1WAY = OFF #pragma config PMDL1WAY = OFF #pragma config IOL1WAY = ON #pragma config FUSBIDIO = OFF /*** BF1SEQ0 ***/ #pragma config TSEQ = 0xffff #pragma config CSEQ = 0xffff // ***************************************************************************** // ***************************************************************************** // Section: Library/Stack Initialization Data // ***************************************************************************** // *****************************************************************************/ //<editor-fold defaultstate="collapsed" desc="USB Stack Configuration"> /************************************************** * USB Device Function Driver Init Data **************************************************/ const USB_DEVICE_CDC_INIT cdcInit0 = { .queueSizeRead = 1, .queueSizeWrite = 1, .queueSizeSerialStateNotification = 1 }; /************************************************** * USB Device Layer Function Driver Registration * Table **************************************************/ const USB_DEVICE_FUNCTION_REGISTRATION_TABLE funcRegistrationTable[1] = { /* Function 1 */ { .configurationValue = 1, /* Configuration value */ .interfaceNumber = 0, /* First interfaceNumber of this function */ .numberOfInterfaces = 2, /* Number of interfaces */ .speed = USB_SPEED_HIGH|USB_SPEED_FULL, /* Function Speed */ .funcDriverIndex = 0, /* Index of CDC Function Driver */ .driver = (void*)USB_DEVICE_CDC_FUNCTION_DRIVER, /* USB CDC function data exposed to device layer */ .funcDriverInit = (void*)&cdcInit0 /* Function driver init data */ }, }; /******************************************* * USB Device Layer Descriptors *******************************************/ /******************************************* * USB Device Descriptor for this demo *******************************************/ const USB_DEVICE_DESCRIPTOR deviceDescriptor= { 0x12, // Size of this descriptor in bytes USB_DESCRIPTOR_DEVICE, // DEVICE descriptor type 0x0200, // USB Spec Release Number in BCD format USB_CDC_CLASS_CODE, // Class Code USB_CDC_SUBCLASS_CODE, // Subclass code 0x00, // Protocol code USB_DEVICE_EP0_BUFFER_SIZE, // Max packet size for EP0, see system_config.h 0x04D8, // Vendor ID 0x000A, // Product ID: CDC RS-232 Emulation Demo 0x0100, // Device release number in BCD format 0x01, // Manufacturer string index 0x02, // Product string index 0x00, // Device serial number string index 0x01 // Number of possible configurations }; /******************************************* * USB Device Qualifier Descriptor for this * demo. *******************************************/ const USB_DEVICE_QUALIFIER deviceQualifierDescriptor1 = { 0x0A, // Size of this descriptor in bytes USB_DESCRIPTOR_DEVICE_QUALIFIER, // Device Qualifier Type 0x0200, // USB Specification Release number 0x00, // Class Code 0x00, // SubClass Code 0x00, // Protocol code 0x64, // Maximum packet size for endpoint 0 0x01, // Number of possible configurations 0x00 // Reserved for future use. }; /******************************************* * High Speed Configuration Descriptor *******************************************/ const uint8_t highSpeedConfigurationDescriptor1[]= { /* Configuration Descriptor Header */ 0x09, // Size of this descriptor USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type 67,0, // Total length of data for this configuration 2, // Number of interfaces in this configuration 1, // Index value of this configuration 0, // Configuration string index USB_ATTRIBUTE_DEFAULT | USB_ATTRIBUTE_SELF_POWERED, // Attributes, see usb_device.h 50, // Max power consumption (2X mA) /* Interface Descriptor 1 */ 9, // Size of the descriptor USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type 0, // Interface Number 0, // Alternate Setting Number 1, // Number of endpoints in this intf USB_CDC_COMMUNICATIONS_INTERFACE_CLASS_CODE, // Class code USB_CDC_SUBCLASS_ABSTRACT_CONTROL_MODEL, // Subclass code USB_CDC_PROTOCOL_AT_V250, // Protocol code 0, // Interface string index /* CDC Class-Specific Descriptors */ sizeof(USB_CDC_HEADER_FUNCTIONAL_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_HEADER, // Type of functional descriptor 0x20,0x01, // CDC spec version sizeof(USB_CDC_ACM_FUNCTIONAL_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_ABSTRACT_CONTROL_MANAGEMENT, // Type of functional descriptor USB_CDC_ACM_SUPPORT_LINE_CODING_LINE_STATE_AND_NOTIFICATION, // bmCapabilities of ACM sizeof(USB_CDC_UNION_FUNCTIONAL_DESCRIPTOR_HEADER) + 1, // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_UNION, // Type of functional descriptor 0, // com interface number 1, sizeof(USB_CDC_CALL_MANAGEMENT_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_CALL_MANAGEMENT, // Type of functional descriptor 0x00, // bmCapabilities of CallManagement 1, // Data interface number /* Interrupt Endpoint (IN) Descriptor */ 0x07, // Size of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x81, // EndpointAddress ( EP1 IN INTERRUPT) USB_TRANSFER_TYPE_INTERRUPT, // Attributes type of EP (INTERRUPT) 0x10,0x00, // Max packet size of this EP 0x02, // Interval (in ms) /* Interface Descriptor */ 9, // Size of this descriptor in bytes USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type 1, // Interface Number 0, // Alternate Setting Number 2, // Number of endpoints in this interface USB_CDC_DATA_INTERFACE_CLASS_CODE, // Class code 0, // Subclass code USB_CDC_PROTOCOL_NO_CLASS_SPECIFIC, // Protocol code 0, // Interface string index /* Bulk Endpoint (OUT) Descriptor */ 0x07, // Sizeof of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x02, // Endpoint Address BULK OUT USB_TRANSFER_TYPE_BULK, // Attributes BULK EP 0x00,0x02, // MaxPacket Size of EP (BULK OUT) 0x00, // Interval Can be ignored for BULK EPs. /* Bulk Endpoint (IN)Descriptor */ 0x07, // Size of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x82, // EndpointAddress BULK IN USB_TRANSFER_TYPE_BULK, // Attributes BULK EP 0x00,0x02, // MaxPacket Size of EP (BULK IN) 0x00, // Interval Can be ignored for BULK EPs. }; /******************************************* * Array of High speed config descriptors *******************************************/ USB_DEVICE_CONFIGURATION_DESCRIPTORS_TABLE highSpeedConfigDescSet[1] = { highSpeedConfigurationDescriptor1 }; /******************************************* * Full Speed Configuration Descriptor *******************************************/ const uint8_t fullSpeedConfigurationDescriptor1[]= { /* Configuration Descriptor Header */ 0x09, // Size of this descriptor USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type 67,0, // Total length of data for this configuration 2, // Number of interfaces in this configuration 1, // Index value of this configuration 0, // Configuration string index USB_ATTRIBUTE_DEFAULT | USB_ATTRIBUTE_SELF_POWERED, // Attributes, see usb_device.h 50, // Max power consumption (2X mA) /* Interface Descriptor 1 */ 9, // Size of the descriptor USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type 0, // Interface Number 0, // Alternate Setting Number 1, // Number of endpoints in this intf USB_CDC_COMMUNICATIONS_INTERFACE_CLASS_CODE, // Class code USB_CDC_SUBCLASS_ABSTRACT_CONTROL_MODEL, // Subclass code USB_CDC_PROTOCOL_AT_V250, // Protocol code 0, // Interface string index /* CDC Class-Specific Descriptors */ sizeof(USB_CDC_HEADER_FUNCTIONAL_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_HEADER, // Type of functional descriptor 0x20,0x01, // CDC spec version sizeof(USB_CDC_ACM_FUNCTIONAL_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_ABSTRACT_CONTROL_MANAGEMENT, // Type of functional descriptor USB_CDC_ACM_SUPPORT_LINE_CODING_LINE_STATE_AND_NOTIFICATION, // bmCapabilities of ACM sizeof(USB_CDC_UNION_FUNCTIONAL_DESCRIPTOR_HEADER) + 1, // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_UNION, // Type of functional descriptor 0, // com interface number 1, sizeof(USB_CDC_CALL_MANAGEMENT_DESCRIPTOR), // Size of the descriptor USB_CDC_DESC_CS_INTERFACE, // CS_INTERFACE USB_CDC_FUNCTIONAL_CALL_MANAGEMENT, // Type of functional descriptor 0x00, // bmCapabilities of CallManagement 1, // Data interface number /* Interrupt Endpoint (IN) Descriptor */ 0x07, // Size of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x81, // EndpointAddress ( EP1 IN INTERRUPT) USB_TRANSFER_TYPE_INTERRUPT, // Attributes type of EP (INTERRUPT) 0x10,0x00, // Max packet size of this EP 0x02, // Interval (in ms) /* Interface Descriptor */ 9, // Size of this descriptor in bytes USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type 1, // Interface Number 0, // Alternate Setting Number 2, // Number of endpoints in this interface USB_CDC_DATA_INTERFACE_CLASS_CODE, // Class code 0, // Subclass code USB_CDC_PROTOCOL_NO_CLASS_SPECIFIC, // Protocol code 0, // Interface string index /* Bulk Endpoint (OUT) Descriptor */ 0x07, // Sizeof of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x02, // Endpoint Address BULK OUT USB_TRANSFER_TYPE_BULK, // Attributes BULK EP 0x40,0x00, // MaxPacket Size of EP (BULK OUT) 0x00, // Interval Can be ignored for BULK EPs. /* Bulk Endpoint (IN)Descriptor */ 0x07, // Size of this descriptor in bytes USB_DESCRIPTOR_ENDPOINT, // Endpoint Descriptor 0x82, // EndpointAddress BULK IN USB_TRANSFER_TYPE_BULK, // Attributes BULK EP 0x40,0x00, // MaxPacket Size of EP (BULK IN) 0x00, // Interval Can be ignored for BULK EPs. }; /******************************************* * Array of High speed config descriptors *******************************************/ USB_DEVICE_CONFIGURATION_DESCRIPTORS_TABLE fullSpeedConfigDescSet[1] = { fullSpeedConfigurationDescriptor1 }; /************************************** * String descriptors. *************************************/ /* Language code string descriptor 0 */ const struct { uint8_t bLength; uint8_t bDscType; uint16_t string[1]; } sd000 = { sizeof(sd000), // Size of this descriptor in bytes USB_DESCRIPTOR_STRING, // STRING descriptor type {0x0409} // Language ID }; /* Manufacturer string descriptor 1 */ const struct { uint8_t bLength; // Size of this descriptor in bytes uint8_t bDscType; // STRING descriptor type uint16_t string[25]; // String } sd001 = { sizeof(sd001), USB_DESCRIPTOR_STRING, {'M','i','c','r','o','c','h','i','p',' ', 'T','e','c','h','n','o','l','o','g','y',' ','I','n','c','.'} }; /* Product string descriptor 2 */ const struct { uint8_t bLength; // Size of this descriptor in bytes uint8_t bDscType; // STRING descriptor type uint16_t string[22]; // String } sd002 = { sizeof(sd002), USB_DESCRIPTOR_STRING, {'S','i','m','p','l','e',' ','C','D','C',' ', 'D','e','v','i','c','e',' ','D','e','m','o' } }; /*************************************** * Array of string descriptors ***************************************/ USB_DEVICE_STRING_DESCRIPTORS_TABLE stringDescriptors[3]= { (const uint8_t *const)&sd000, (const uint8_t *const)&sd001, (const uint8_t *const)&sd002 }; /******************************************* * USB Device Layer Master Descriptor Table *******************************************/ const USB_DEVICE_MASTER_DESCRIPTOR usbMasterDescriptor = { &deviceDescriptor, // Full speed descriptor. 1, // Total number of full speed configurations available . fullSpeedConfigDescSet, // Pointer to array of full speed configurations descriptors. &deviceDescriptor, // High speed device descriptor. 1, // Total number of high speed configurations available. highSpeedConfigDescSet, // Pointer to array of high speed configurations descriptors. 3, // Total number of string descriptors available. stringDescriptors, // Pointer to array of string descriptors. &deviceQualifierDescriptor1,// Pointer to full speed dev qualifier. &deviceQualifierDescriptor1 // Pointer to high speed dev qualifier. }; /**************************************************** * USB Device Layer Initialization Data ****************************************************/ const USB_DEVICE_INIT usbDevInitData = { /* System module initialization */ .moduleInit = {SYS_MODULE_POWER_RUN_FULL}, /* Identifies peripheral (PLIB-level) ID */ .usbID = 0, /* Stop in idle */ .stopInIdle = false, /* Suspend in sleep */ .suspendInSleep = false, /* Interrupt Source for USB module */ .interruptSource = INT_SOURCE_USB_1, /* Interrupt Source for USB module */ .interruptSourceUSBDma = INT_SOURCE_USB_1_DMA, /* Number of function drivers registered to this instance of the USB device layer */ .registeredFuncCount = 1, /* Function driver table registered to this instance of the USB device layer*/ .registeredFunctions = (USB_DEVICE_FUNCTION_REGISTRATION_TABLE*)funcRegistrationTable, /* Pointer to USB Descriptor structure */ .usbMasterDescriptor = (USB_DEVICE_MASTER_DESCRIPTOR*)&usbMasterDescriptor, /* USB Device Speed */ .deviceSpeed = USB_SPEED_HIGH, }; // </editor-fold> // ***************************************************************************** // ***************************************************************************** // Section: Driver Initialization Data // ***************************************************************************** // ***************************************************************************** //<editor-fold defaultstate="collapsed" desc="DRV_Timer Configuration"> /*** TMR Driver Initialization Data ***/ const DRV_TMR_INIT drvTmr0InitData = { .moduleInit.sys.powerState = DRV_TMR_POWER_STATE_IDX0, .tmrId = DRV_TMR_PERIPHERAL_ID_IDX0, .clockSource = DRV_TMR_CLOCK_SOURCE_IDX0, .prescale = DRV_TMR_PRESCALE_IDX0, .mode = DRV_TMR_OPERATION_MODE_IDX0, .interruptSource = DRV_TMR_INTERRUPT_SOURCE_IDX0, .asyncWriteEnable = false, }; // </editor-fold> //<editor-fold defaultstate="collapsed" desc="SYS_TMR Configuration"> /*** TMR Service Initialization Data ***/ const SYS_TMR_INIT sysTmrInitData = { .moduleInit = {SYS_MODULE_POWER_RUN_FULL}, .drvIndex = DRV_TMR_INDEX_0, .tmrFreq = 1000, }; // </editor-fold> // ***************************************************************************** // ***************************************************************************** // Section: System Data // ***************************************************************************** // ***************************************************************************** /* Structure to hold the object handles for the modules in the system. */ SYSTEM_OBJECTS sysObj; // ***************************************************************************** // ***************************************************************************** // Section: Module Initialization Data // ***************************************************************************** // ***************************************************************************** //<editor-fold defaultstate="collapsed" desc="SYS_DEVCON Configuration"> /*** System Device Control Initialization Data ***/ const SYS_DEVCON_INIT sysDevconInit = { .moduleInit = {0}, }; // </editor-fold> // ***************************************************************************** // ***************************************************************************** // Section: Static Initialization Functions // ***************************************************************************** // ***************************************************************************** // ***************************************************************************** // ***************************************************************************** // Section: System Initialization // ***************************************************************************** // ***************************************************************************** /******************************************************************************* Function: void SYS_Initialize ( SYS_INIT_DATA *data ) Summary: Initializes the board, services, drivers, application and other modules. Remarks: See prototype in system/common/sys_module.h. */ void SYS_Initialize ( void* data ) { /* Core Processor Initialization */ SYS_CLK_Initialize( NULL ); sysObj.sysDevcon = SYS_DEVCON_Initialize(SYS_DEVCON_INDEX_0, (SYS_MODULE_INIT*)&sysDevconInit); SYS_DEVCON_PerformanceConfig(SYS_CLK_SystemFrequencyGet()); SYS_PORTS_Initialize(); /* Board Support Package Initialization */ BSP_Initialize(); /* Initialize Drivers */ sysObj.drvTmr0 = DRV_TMR_Initialize(DRV_TMR_INDEX_0, (SYS_MODULE_INIT *)&drvTmr0InitData); SYS_INT_VectorPrioritySet(INT_VECTOR_T2, INT_PRIORITY_LEVEL4); SYS_INT_VectorSubprioritySet(INT_VECTOR_T2, INT_SUBPRIORITY_LEVEL0); /* Initialize System Services */ SYS_INT_Initialize(); /*** TMR Service Initialization Code ***/ sysObj.sysTmr = SYS_TMR_Initialize(SYS_TMR_INDEX_0, (const SYS_MODULE_INIT * const)&sysTmrInitData); /* Initialize Middleware */ /* Set priority of USB interrupt source */ SYS_INT_VectorPrioritySet(INT_VECTOR_USB1, INT_PRIORITY_LEVEL4); /* Set Sub-priority of USB interrupt source */ SYS_INT_VectorSubprioritySet(INT_VECTOR_USB1, INT_SUBPRIORITY_LEVEL0); /* Set the priority of the USB DMA Interrupt */ SYS_INT_VectorPrioritySet(INT_VECTOR_USB1_DMA, INT_PRIORITY_LEVEL4); /* Set Sub-priority of the USB DMA Interrupt */ SYS_INT_VectorSubprioritySet(INT_VECTOR_USB1_DMA, INT_SUBPRIORITY_LEVEL0); /* Initialize the USB device layer */ sysObj.usbDevObject0 = USB_DEVICE_Initialize (USB_DEVICE_INDEX_0 , ( SYS_MODULE_INIT* ) & usbDevInitData); /* Enable Global Interrupts */ SYS_INT_Enable(); /* Initialize the Application */ APP_Initialize(); } /******************************************************************************* End of File */ //以下、 1lcd_lib_C32.h //---------------------------------------------------------------------------------- //************************************************************************* //インクルードファイル 1lcd_lib_C32.h //このファイルは後閑哲也さんが設計されたCCSコンパイラ用液晶表示ライブラリ 1lcd_lib.cをもとに、 //C32コンパイラ対応等で変更したものです。 //************************************************************************* #include <p32xxxx.h> //#include <proc/p32mx795f512l.h> //MX795はイニシャライズループを抜けられない //#include <proc/p32mx360f512l.h> //PIC32MX460F512L #define lcd_Clock 200000000 // 単位はHzで指定 // LCDポート設定 #define lcd_port_DB7 LATEbits.LATE4 //LCDのDB7(14番ピン)に接続されるPIC側ポート番号設定 #define lcd_port_DB6 LATEbits.LATE3 //LCDのDB6(13番ピン)に接続されるPIC側ポート番号設定 #define lcd_port_DB5 LATEbits.LATE2 //のDB5(12番ピン)に接続されるPIC側ポート番号設定 #define lcd_port_DB4 LATEbits.LATE1 //LCDのDB4(11番ピン)に接続されつPIC側ポート番号設定 #define lcd_rs LATGbits.LATG12 //LCDのrs(4番ピン)に接続されるPIC側ポート番号設定 #define lcd_stb LATGbits.LATG13 //LCDのstb(6番ピン)に接続されるPIC側ポート番号設定 void lcd_out(char code, char flag); void lcd_data(char asci); void lcd_cmd(char cmd); void lcd_clear(void); void lcd_init(void); void lcd_str(char *str); //以下、1lcd_lib_C32.c //--------------------------------------------------------------------------------------- //************************************************************************** //インクルードファイル 1lcd_lib_C32.c //このファイルは後閑哲也さんが設計されたCCSコンパイラ用液晶表示ライブラリ 1lcd_lib.cをもとに、 //C32コンパイラ対応等で変更したものです。 //************************************************************************** /////////////////////////////////////////////// // 液晶表示器制御ライブラリ for C32コンパイラー // 内蔵関数は以下 // lcd_init() ----- 初期化 // lcd_cmd(cmd) ----- コマンド出力 // lcd_data(chr) ----- 1文字表示出力 // lcd_clear() ----- 全消去 // lcd_str(str*) ----- 文字列表示 ////////////////////////////////////////////// #include "1lcd_lib_C32.h" void lcd_delay_us(unsigned int usec) //1μsec遅延関数 { int count; count = (int)(lcd_Clock/20000000)*usec; do //実測: at 200MH (Clock=200000000) { //delay_us(1000)→1000.4μsec、 delay_us(100)→100.6μsec、delay_us(10)→10.5μsec、delay_us(1)→1.5μsec asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); count--; }while(count != 0); } void lcd_delay_ms(unsigned int msec) //1msec遅延関数 { unsigned int i; for(i=0; i<msec; i++) lcd_delay_us(1000); } //////// データ出力サブ関数 void lcd_out(char code, char flag) { if(code & 0b10000000)lcd_port_DB7 = 1; //LCDのDB7への出力セット else lcd_port_DB7 = 0; if(code & 0b01000000)lcd_port_DB6 = 1; //LCDのDB6への出力セット else lcd_port_DB6 = 0; if(code & 0b00100000)lcd_port_DB5 = 1; //LCDのDB5への出力セット else lcd_port_DB5 = 0; if(code & 0b00010000)lcd_port_DB4 = 1; //LCDのDB4への出力セット else lcd_port_DB4 = 0; if (flag == 0) lcd_rs = 1; // 表示データの場合 else lcd_rs = 0; // コマンドデータの場合 lcd_delay_us(3); //1μsecウェイト // lcd_delay_us(1); //1μsecウェイト lcd_stb = 1; // strobe(E) ON (Enable) lcd_delay_us(3); //1μsecウェイト // lcd_delay_us(1); // 1μsec : strobe信号の幅 lcd_stb = 0; // reset strobe } //////// 1文字表示関数 void lcd_data(char asci) { lcd_out(asci, 0); // 上位4ビット出力 lcd_out(asci<<4, 0); // 下位4ビット出力 lcd_delay_us(50); //50μsecウェイト } /////// コマンド出力関数 void lcd_cmd(char cmd) { lcd_out(cmd, 1); // 上位4ビット出力 lcd_out(cmd<<4, 1); // 下位4ビット出力 if((cmd & 0x03) != 0) // clear Homeの場合 lcd_delay_ms(2); // 2msec待ち else lcd_delay_us(50); //50μsecウェイト } /////// 全消去関数 void lcd_clear(void) { lcd_cmd(0x01); // 初期化コマンド出力 } /////// 文字列出力関数 void lcd_str(char* str) { while(*str) //文字列終端の '\0'を検出するまで { lcd_data(*str); // 1文字表示 str++; //ポインタをインクリメント } } /////// 初期化関数 void lcd_init(void) { lcd_delay_ms(20); //20msecウェイト lcd_out(0x30, 1); // 8bit mode set lcd_delay_ms(5); //5msecウェイト lcd_out(0x30, 1); // 8bit mode set lcd_delay_ms(1); //1msecウェイト lcd_out(0x30, 1); // 8bit mode set lcd_delay_ms(1); //1msecウェイト lcd_out(0x20, 1); // 4bit mode set lcd_delay_ms(1); //1msecウェイト lcd_cmd(0x2E); // DL=0 4bit mode lcd_cmd(0x08); // display off C=D=B=0 lcd_cmd(0x0D); // display on C=D=1 B=0 lcd_cmd(0x06); // entry I/D=1 S=0 lcd_cmd(0x02); // cursor home }
<動作結果>
1. ピクチャー
PIC側液晶画面 上段: 受信文字列 下段: 返信文字列 |
PC画面 | |
送信前 | ||
PCから "Japan\r"を 送信した場合 |
||
PCから 以下の順番で 文字列を送信した場合 "Japan\r" "America\r" "U.K.\r" "France\r" |
2. 動画 -->YouTube